package com.boot.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.boot.web.dao.SpecAttributeDao;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.SpecAttribute;
import com.boot.web.service.SpecAttributeService;

/**
 * 规格属性控制器
 * @author 【zhouJie】
 * @date 2021-06-02 08:43:22
 */
@RestController
@RequestMapping("/specAttribute")
public class SpecAttributeController {

    @Autowired
    private SpecAttributeService specAttributeService;

    @Autowired
    private SpecAttributeDao specAttributeDao;

    @PostMapping("/save")
    public ResponseServer save(SpecAttribute specAttribute) {
        specAttribute.setUpdateTime(new Date());
        return ResponseServer.success("添加成功", specAttributeService.save(specAttribute));
    }

    @PutMapping("/update")
    public ResponseServer update(SpecAttribute specAttribute) {
        specAttribute.setUpdateTime(new Date());
        if("".equals(specAttribute.getAttributeName())){
            specAttributeService.update(specAttribute);
        }else{
            if(specAttribute.getClassifyId() == null){
                specAttributeService.update(specAttribute);
            }else{
                SpecAttribute newSa = new SpecAttribute();
                newSa.setAttributeName(specAttribute.getAttributeName());
                newSa.setParentId(specAttribute.getId());
                newSa.setUpdateTime(new Date());
                specAttributeService.save(newSa);
            }
        }
        return ResponseServer.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        List<SpecAttribute> list = specAttributeDao.querySpecAttributeByParentId(id);
        if(list.size() > 0){
            for(int i = 0; i < list.size(); i++){
                specAttributeDao.delete(list.get(i).getId());
            }
        }
        return ResponseServer.success("删除成功", specAttributeService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        for(int j = 0; j < ids.length; j++){
            List<SpecAttribute> list = specAttributeDao.querySpecAttributeByParentId(ids[j]);
            if(list.size() > 0){
                for(int i = 0; i < list.size(); i++){
                    specAttributeService.delete(list.get(i).getId());
                }
                specAttributeService.delete(ids[j]);
            }else{
                specAttributeService.delete(ids[j]);
            }
        }
        return ResponseServer.success("批量删除成功");
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(specAttributeService.getSpecAttributeVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(specAttributeDao.queryAllVO());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(specAttributeService.getById(id));
    }

}
